<?php

class Admin_FaculdadeprovaController extends App_Controller_Action
{
    private $_table     = 'FACULDADE_PROVA';
    private $_pk        = 'FAP_ID';
    private $_order_col = 'FAP_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'FAP_STA';
    private $obj        = '';
    private $form       = '';
    
    public function init()
    {
        $this->obj = new Admin_Model_DbTable_Faculdade();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Faculdadeprova();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('elemform','library');
        
        $fac_id = $this->_getParam('fac_id', 0);
        if($fac_id == 0) $this->_helper->redirector('index','faculdade',$this->modulo,array());
        $this->view->fac_id = $fac_id;
        
        $where = $this->_pk . ' > 0 AND fp.FAC_ID = '.$fac_id;
        
        //$filtro = $this->_getParam('itfiltro', '1'); 
        //if($filtro != '') $where .= ' and MTL_STA = ' . $filtro;
        //$this->view->filtro = $filtro;
        
        //$STA = $this->_getParam($this->_sta_col, 1);
        //$where .= ' and ' . $this->_sta_col . ' = ' . $STA;
        
        $query = 'SELECT SQL_CACHE fp.*,f.FAC_NOM FROM '.$this->_table.' fp, FACULDADE f  WHERE fp.FAC_ID = f.FAC_ID AND '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
    
    public function editAction() {
        
        Zend_Loader::loadClass('tools','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_form_pdr.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_faculdadeprova.js?1=6');
        
        $fac_id = $this->_getParam('fac_id', 0);
        if($fac_id == 0) $this->_helper->redirector('index','faculdade',$this->modulo,array());
        
        $query = 'SELECT SQL_CACHE * FROM DISCIPLINA WHERE DCP_STA = 1 ORDER BY DCP_NOM';
        $result = $this->db->fetchAll($query);
        $html = '<select name="d[]" class="tam_med_g" ><option value="">Nenhum</option>';
        foreach($result as $row){
            $html .= '<option value="'.$row['DCP_ID'].'">'.$row['DCP_NOM'].'</option>';
        }
        $html .= '</select>';
        echo '<script> var cmb_dis = \''.$html.'\' </script>';
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){//$this->form->isValid($formData)) {
                    
                    $this->db->beginTransaction();
                    
                    $id = (int) $formData[$this->_pk];//$this->form->getValue($this->_pk);
                    
                    $data = $formData;//$this->form->getValues();
                    unset($data['submit']);
                    
                    unset($data['html']);
                    unset($data['d']);
                    unset($data['n']);
                    unset($data['p']);
                    
                    $data['FAC_ID'] = $fac_id;
                    
                    if ($id == 0) {
                        $this->db->insert($this->_table,$data);
                        $id = $this->db->lastInsertId();
                    }
                    else $this->db->update($this->_table,$data, $this->_pk.' = ' . $id);
                    
                    $this->db->delete('FACULDADE_PROVA_DISC',$this->_pk.' = ' . $id);
                    if(isset($formData['d']))
                    foreach($formData['d'] as $key => $d){
                        
                        if($d > 0){
                            
                            $data2 = array(
                                'DCP_ID'   => $d,
                                'FAP_ID'    => $id,
                                'FPD_NQU'   => $formData['n'][$key],
                                'FPD_PES'   => $formData['p'][$key],
                            );

                            $this->db->insert('FACULDADE_PROVA_DISC',$data2);
                            
                        }
                    }
                    
                    
                    $this->db->commit();
                    
                    $urlOptions = array('controller'=>$this->_controller,'action'=>'index','fac_id'=>$fac_id);
                    $this->_helper->redirector->gotoRoute($urlOptions);
                    
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                $this->form->populate($formData);
                $this->db->rollBack();
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id;
                $data = $this->db->fetchRow($query);
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $msg = '';
        $id = $this->_getParam('id', 0);
        if((int) $id > 0 ) {   
            try {
                $this->db->beginTransaction();
                $ok = (bool) $this->db->delete($this->_table,$this->_pk . ' = ' . $id);
                $this->db->commit();
            } catch(Exception $ex) {
                $this->db->rollBack();
                $msg = 'Prova possui disciplinas cadastradas!';
            }
        }       
        echo json_encode(array('ok' => $ok, 'msg'=>$msg));
    }
    
}
